import $copy from '@utils/copy'
import onInit from '../_utils/edit'
import { dataTypeData } from './data-type'
import { createMigration } from '../_utils/create-migration-tool'
const { $, $http } = window

const Index = ({ table_name, table_comment, folder, tableRef }) => ({
	modal: {
		title 		: table_name,
		width 		: '100%',
		height 		: '100%',
		maxWidth 	: null,
		controls 	: {
			after: [
				{ label:'复制 Table', ghost:1, click: () => $copy( window.$formModalRef().formRef().getValue('table') ) },
				{ label:'复制 Migration', ghost:1, click: () => $copy( window.$formModalRef().formRef().getValue('migration') ) },
				{ label:'复制数据类型', ghost:1, mode:'dropdown', data:dataTypeData, change: ({ value }) => {
					$copy(value)
				} },
			],
			ok: { label:'保存 Tabel' },
		}
	},
	form: {
		labelWidth 	: 's3',
		className 	: 'pre-form',
		api 		: 'file/save_sql_table',
		infoApi 	: 'file/read_table',
		param 		: { folder, table_name},
		setParam 	: ({ migration }) => ({ template:migration,  table: null, migration:null  }),
		backParam 	: { folder, table_name },
		okTxt 		: `表【${table_name}】保存成功`,
		// close 		: false,
		data: [
			{ name:'migration', type:'textarea', maxLength: null, p:'Migration', r:1, _label: 'Migration 表', class:'jz-migration-textarea'},
			{ name:'table', type:'textarea', maxLength: null, p:'Table', _label: 'Table 表', class:'jz-table-textarea' },
		],
		onInit: () => {
			onInit('.jz-table-textarea')
			onInit('.jz-migration-textarea')
		}
	},
	onEnd: ({ formRef }) =>{
		const data = $.ref(tableRef).tableRef().data()
		$http.pull(null, 'template/table', { loading:true }).then( d => {
			let migration = createMigration({ data, table_name, table_comment, template:d.content })
			formRef.setValue({ migration })
		} )
	}
})
export default Index